#include "audittrailmodel.h"
#include <QDateTime>

AuditTrailModel::AuditTrailModel(AuditLogger *logger, QObject *parent)
    : QAbstractTableModel(parent), m_logger(logger)
{
    refresh();
}

int AuditTrailModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_records.size();
}

int AuditTrailModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return 6; // Timestamp, User, Action, Table, Record ID, Description
}

QVariant AuditTrailModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_records.size())
        return QVariant();

    const AuditLogger::AuditRecord &record = m_records.at(index.row());

    if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
        switch (index.column()) {
        case 0: // Timestamp
            return record.timestamp.toString("yyyy-MM-dd HH:mm:ss");
        case 1: // User
            return record.userId;
        case 2: // Action
            switch (record.action) {
            case AuditLogger::ActionType::Create: return tr("Create");
            case AuditLogger::ActionType::Read: return tr("Read");
            case AuditLogger::ActionType::Update: return tr("Update");
            case AuditLogger::ActionType::Delete: return tr("Delete");
            case AuditLogger::ActionType::Login: return tr("Login");
            case AuditLogger::ActionType::Logout: return tr("Logout");
            default: return tr("System");
            }
        case 3: // Table
            return record.tableName;
        case 4: // Record ID
            return record.recordId;
        case 5: // Description
            return record.description;
        default:
            return QVariant();
        }
    } else if (role == Qt::UserRole) {
        // 返回原始数据用于排序
        switch (index.column()) {
        case 0: return record.timestamp;
        case 1: return record.userId;
        case 2: return static_cast<int>(record.action);
        case 3: return record.tableName;
        case 4: return record.recordId;
        case 5: return record.description;
        default: return QVariant();
        }
    }

    return QVariant();
}

QVariant AuditTrailModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
        case 0: return tr("Timestamp");
        case 1: return tr("User");
        case 2: return tr("Action");
        case 3: return tr("Table");
        case 4: return tr("Record ID");
        case 5: return tr("Description");
        default: return QVariant();
        }
    }
    return QVariant();
}

void AuditTrailModel::refresh()
{
    beginResetModel();
    m_records = m_logger->queryEvents();
    endResetModel();
}

void AuditTrailModel::setAuditRecords(const QVector<AuditLogger::AuditRecord> &records)
{
    beginResetModel();
    m_records = records;
    endResetModel();
}